var express = require('express');
var router = express.Router();
const db=require('../db/db');
const common=require('../lib/common')
const Token=require('../lib/token')

// 用户登录
router.post('/login',function (req,res) {
    const {username,password}=req.body;
    if (username==null||password==null){
        res.send({flag:false,msg:'用户名或密码不能为空'});
        return;
    }else if (username.trim()==""||password.trim()==""){
        res.send({flag:false,msg:'用户名或密码不能为空'});
        return;
    }
    let sql=`select * from user where username=?`;
    db.query(sql,username,function (err,result) {
        if (err){
            console.log('查询出错：'+err);
            res.send({flag:false,msg:'用户名或密码不能为空'});
            return;
        }else{
            if (result.length==0){
                res.send({flag:false,msg:'账号不存在'});
            }else if(result[0].password.toLocaleUpperCase()!=common.md5(password).toLocaleUpperCase()){
                res.send({flag:false,msg:'密码错误'});
            }else{
                let tokenKey = Token.createToken(result[0].user_address)
                res.send({
                    flag:true,
                    msg:'登录成功',
                    result:result,
                    token:"Bearer "+tokenKey
                });
            }
        }
    })
});

// 获取用户名
router.post('/decodeUser_address', (req, res)=>{
    const token=String(req.headers.authorization).split(' ').pop()
    let data = Token.decode(token)
    if (data.status=='failed'){
        res.send({flag:false,msg:data.msg})
    }else {
        res.json({
            user_address:data.data
        })
    }
})

//注册
router.post('/register',function (req,res) {
    const {username,password,user_address}=req.body;
    if (username==null||password==null){
        res.send({flag:false,msg:'用户名不能为空'});
        return;
    }else if (username.trim()==""||password.trim()==""){
        res.send({flag:false,msg:'用户名不能为空'});
        return;
    }
    let sql=`select * from user where user_address=?`;
    db.query(sql,user_address,function (err,result) {
        if (err){
            console.log('查询出错：'+err);
            res.send({flag:false,msg:'用户名不能为空'});
            return;
        }else{
            if (result.length>0){
                res.send({flag:false,msg:'该钱包已经注册过了'});
            }else {
                sql=`select * from user where user_address=?`
                db.query(sql,username,function (err,result) {
                    if (err){
                        console.log('查询出错：'+err);
                        return;
                    }else{
                        if (result.length>0){
                            res.send({flag:false,msg:'用户名已存在'});
                        }else {
                            sql=`insert into user(user_address,username,password) values(?,?,?)`
                            db.query(sql,[user_address,username,common.md5(password)],function (err,result) {
                                if (err){
                                    console.log('查询出错：'+err);
                                    res.send({flag:false,msg:'用户名或密码格式错误'});
                                    return;
                                }else{
                                    if (result.length<1){
                                        res.send({flag:false,msg:'注册失败'});
                                        return;
                                    }
                                    res.send({flag:true,msg:'注册成功'});
                                }
                            })
                        }
                    }
                })
            }
        }
    })
});

//第一次登录识别
router.post('/saveUser', function(req, res, next) {
    let {user_address}=req.body
    if (user_address==null){
        res.send({flag:false,msg:'地址获取不到'});
        return
    }else if (user_address.trim()==""){
        res.send({flag:false,msg:'地址获取不到'});
        return
    }
    let sql=`select * from user where user_address=?`;
    db.query(sql,[user_address],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            if (result.length==0){
                sql=`insert into user(user_address) values(?)`
                db.query(sql,[user_address],function (err,result) {
                    if (err){
                        console.log(err);
                        res.send({flag:false,msg:'获取失败'});
                        return;
                    }else {
                        if (result.length<1){
                            res.send({flag:false,msg:'登录失败'});
                            return;
                        }
                        let tokenKey = Token.createToken(user_address)
                        res.send({
                            flag:true,
                            msg:'登录成功',
                            token:"Bearer "+tokenKey
                        });
                    }
                })
            }else {
                if (result.length<1){
                    res.send({flag:false,msg:'获取失败'});
                    return;
                }
                let tokenKey = Token.createToken(user_address)
                res.send({
                    flag:true,
                    msg:'登录成功',
                    token:"Bearer "+tokenKey
                });
            }
        }
    })
});

// 获取个人信息
router.post('/getMyInformation', function(req, res, next) {
    let user_address=""
    const token=String(req.headers.authorization).split(' ').pop()
    if (token=="undefined"){
        res.send({flag:false,msg:"未登录无权限访问"})
        return;
    }
    let data = Token.decode(token)
    if (data.status=="failed"){
        res.send({flag:false,msg:data.msg})
        return;
    }else {
        user_address=data.data
    }
    let sql="select a.*,\n" +
        "(SELECT count(id) FROM focus_user where user_id=?) as number_concerns,\n" +
        "(SELECT count(id) FROM focus_user where focus_user_id=?) as number_fans,\n" +
        "(SELECT count(id) FROM shop where creator_address=? and status='selling') as number_selling,\n" +
        "(SELECT count(id) FROM shop where creator_address=? and nft_id is not null) as number_nft,\n" +
        "count(b.id) as number_array \t\n" +
        "from user a\n" +
        "left join  array b on a.user_address=b.owner\n" +
        "where a.user_address=?\n" +
        "GROUP BY a.id "
    db.query(sql,[user_address,user_address,user_address,user_address,user_address],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            if (result.length<1){
                res.send({flag:false,msg:'获取失败'});
                return;
            }
            res.json(result)
        }
    })
});

//获取背景图
router.post('/getBackground', function(req, res, next) {
    let {id}=req.body
    if (id==null){
        res.send({flag:false,msg:'获取失败'});
        return;
    }else if (id.trim()==""){
        res.send({flag:false,msg:'获取失败'});
        return;
    }
    let sql="select background from user where id=?"
    db.query(sql,[id],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            res.json(result)
        }
    })
});

//上传背景图
router.post('/updateBackground', function(req, res, next) {
    let {id,background}=req.body
    if (id==null||background==null){
        res.send({flag:false,msg:'获取失败'});
        return;
    }else if (id.trim()==""||background.trim()==""){
        res.send({flag:false,msg:'获取失败'});
        return;
    }
    let sql="update user set background=?  where id=?"
    db.query(sql,[background,id],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            if (result.length<1){
                res.send({flag:false,msg:'上传失败'});
                return;
            }
            res.send({flag:true,msg:'上传成功'});
        }
    })
});

//更改个人信息
router.post('/updateInformation', function(req, res, next) {
    let {introduction,logo,username,nickname}=req.body
    let user_address=""
    const token=String(req.headers.authorization).split(' ').pop()
    if (token=="undefined"){
        res.send({flag:false,msg:"未登录无权限访问"})
        return;
    }
    let data = Token.decode(token)
    if (data.status=="failed"){
        res.send({flag:false,msg:data.msg})
        return;
    }else {
        user_address=data.data
    }
    let sql=`select * from user where username = ? and user_address != ?`
    if (introduction==null||user_address==null||username==null||nickname==null){
        res.send({flag:false,msg:'信息不能为空'});
        return
    }else if (introduction.trim()==""||user_address.trim()==""||username.trim()==""||nickname.trim()==""){
        res.send({flag:false,msg:'信息不能为空'});
        return
    }
    db.query(sql,[username,user_address],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'添加失败'});
            return;
        }else{
            if (result.length>0){
                res.send({flag:false,msg:'该用户名已经存在'});
                return;
            }else {
                sql=`select * from user where nickname = ? and user_address != ?`
                    db.query(sql,[nickname,user_address],function (err,result) {
                        if(err){
                            console.log(err);
                            res.send({flag:false,msg:'添加失败'});
                            return;
                        }else {
                            if (result.length > 0) {
                                res.send({flag: false, msg: '该昵称已经存在'});
                                return;
                            } else {
                                sql="update user set introduction=?,logo=?,username=?,nickname=? where user_address=?";
                                db.query(sql,[introduction,logo,username,nickname,user_address],function (err,result) {
                                    if(err){
                                        console.log(err);
                                        res.send({flag:false,msg:'添加失败'});
                                        return;
                                    }else{
                                        if (result.length<1){
                                            res.send({flag:false,msg:'修改个人信息失败'});
                                            return;
                                        }
                                        res.send({flag:true,msg:'修改个人信息成功'});
                                    }
                                })
                            }
                        }
                    })
            }
        }
    })
});

//关注别人
router.post('/focus_user', function(req, res, next) {
    let {focus_user_id}=req.body
    let user_address=""
    const token=String(req.headers.authorization).split(' ').pop()
    if (token=="undefined"){
        res.send({flag:false,msg:"未登录无权限访问"})
        return;
    }
    let data = Token.decode(token)
    if (data.status=="failed"){
        res.send({flag:false,msg:data.msg})
        return;
    }else {
        user_address=data.data
    }
    if (user_address==focus_user_id){
        res.send({flag:false,msg:"不能关注自己"})
        return;
    }
    let sql=`SELECT * FROM focus_user where user_id=? and focus_user_id=?`
    if (focus_user_id==null){
        res.send({flag:false,msg:'获取失败'});
        return;
    }else if (focus_user_id.trim()==""){
        res.send({flag:false,msg:'获取失败'});
        return;
    }
    db.query(sql,[user_address,focus_user_id],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            if (result.length==0){
                sql=`insert into focus_user(user_id,focus_user_id) values(?,?) `;
                db.query(sql,[user_address,focus_user_id],function (err,r) {
                    if(err){
                        console.log(err);
                        res.send({flag:false,msg:'获取失败'});
                        return;
                    }else{
                        if (r<0){
                            res.send({flag:false,msg:'关注失败'});
                            return;
                        }
                        res.send({flag:true,msg:'关注成功'});
                    }
                })
            }else {
                sql="delete from focus_user where id=?"
                db.query(sql,result[0].id,function (err,r) {
                    if (err){
                        console.log(err)
                        res.send({flag:false,msg:'获取失败'});
                        return;
                    }else{
                        if (r<0){
                            res.send({flag:false,msg:'取消关注失败'});
                            return;
                        }
                        res.send({flag:true,msg:'取消关注成功'});
                    }
                })
            }

        }
    })
});

//创建合集
router.post('/insertArray', function(req, res, next) {
    let {title,logo,info}=req.body
    let owner=""
    const token=String(req.headers.authorization).split(' ').pop()
    if (token=="undefined"){
        res.send({flag:false,msg:"未登录无权限访问"})
        return;
    }
    let data = Token.decode(token)
    if (data.status=="failed"){
        res.send({flag:false,msg:data.msg})
        return;
    }else {
        owner=data.data
    }
    if (title==null||logo==null||info==null){
        res.send({flag:false,msg:'信息不能为空'});
        return;
    }else if (title.trim()==""||logo.trim()==""||info.trim()==""){
        res.send({flag:false,msg:'信息不能为空'});
        return;
    }
    let hot=0
    let sql="select title from array where title=? and owner=?"
    db.query(sql,[title,owner],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'报错'});
            return;
        }else{
            if (result.length>0){
                res.send({flag:false,msg:'合集名在您的合集里已经存在'});
                return;
            }else {
                sql=`insert into array(owner,title,logo,info,hot) values(?,?,?,?,?)`;
                db.query(sql,[owner,title,logo,info,hot],function (err,r) {
                    if(err){
                        console.log(err);
                        res.send({flag:false,msg:'添加失败'});
                        return;
                    }else{
                        if (r.length<1){
                            res.send({flag:false,msg:'创建合集失败'});
                        }
                        res.send({flag:true,msg:'创建合集成功'});
                    }
                })
            }
        }
    })
});

//编辑合集信息
router.post('/updateArray', function(req, res, next) {
    let {title,logo,info,id}=req.body
    if (id==null||title==null||logo==null||info==null){
        res.send({flag:false,msg:'信息不能为空'});
        return;
    }else if (id.trim()==""||title.trim()==""||logo.trim()==""||info.trim()==""){
        res.send({flag:false,msg:'信息不能为空'});
        return;
    }
    let sql="update array set title=?,logo=?,info=? where id=?"
    db.query(sql,[title,logo,info,id],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            if (result<0){
                res.send({flag:false,msg:'更改失败'});
                return;
            }
            res.send({flag:true,msg:'更改成功'});
        }
    })
});

//获取某个合集信息
router.post('/getArrayDetail', function(req, res, next) {
    let {id}=req.body
    if (id==null){
        res.send({flag:false,msg:'信息不能为空'});
        return;
    }else if (id.trim()==""){
        res.send({flag:false,msg:'信息不能为空'});
        return;
    }
    let sql="select * from array  where id=?"
    db.query(sql,[id],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            if (result.length<1){
                res.send({flag:false,msg:'获取失败'});
                return;
            }
           res.json(result)
        }
    })
});

//访问他人用户中心
router.post('/visitOtherInformation', function(req, res, next) {
    let {other_address}=req.body
    const token=String(req.headers.authorization).split(' ').pop()
    let user_address=""
    if (token=="undefined"){
        res.send({flag:false,msg:"未登录无权限访问"})
        return;
    }
    let data = Token.decode(token)
    if (data.status=="failed"){
        res.send({flag:false,msg:data.msg})
        return;
    }else {
        user_address=data.data
    }
    if (other_address==null){
        res.send({flag:false,msg:'获取失败'});
        return;
    }else if (other_address.trim()==null){
        res.send({flag:false,msg:'获取失败'});
        return;
    }
    let sql="select a.*,COUNT(b.id) as isfocus,\n" +
        "(SELECT count(id) FROM focus_user where user_id=? )as number_concerns,\n" +
        "(SELECT count(id) FROM focus_user where focus_user_id=?) as number_fans,\n" +
        "(SELECT count(id) FROM focus_user where user_id=? and focus_user_id=?) as isfocus,\n" +
        "(SELECT count(id) FROM shop where creator_address=? and status='selling') as number_selling,\n" +
        "(SELECT count(id) FROM shop where creator_address=? and nft_id is not null) as number_nft,\n" +
        "(SELECT count(id) FROM array where owner=?) as number_array\n" +
        "from user a\n" +
        "left join  focus_user b on a.user_address=b.focus_user_id and b.user_id=?\n" +
        "where a.user_address=? \n" +
        "GROUP BY a.id "
    db.query(sql,[other_address,other_address,user_address,other_address,other_address,other_address,other_address,user_address,other_address],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            if (result.length<1){
                res.send({flag:false,msg:'获取信息失败'});
                return;
            }
            res.json(result)
        }
    })
});

//自己的关注
router.post('/getMyFocus', function(req, res, next) {
    let {pageIndex,pageSize}=req.body
    const token=String(req.headers.authorization).split(' ').pop()
    let user_address=""
    if (token=="undefined"){
        res.send({flag:false,msg:"未登录无权限访问"})
        return;
    }
    let data = Token.decode(token)
    if (data.status=="failed"){
        res.send({flag:false,msg:data.msg})
        return;
    }else {
        user_address=data.data
    }
    if (pageIndex==null||pageSize==null){
        res.send({flag:false,msg:'获取失败'});
        return;
    }else if (pageIndex.trim()==""||pageSize.trim()==""){
        res.send({flag:false,msg:'获取失败'});
        return;
    }
    pageIndex=parseInt(pageIndex);
    pageSize=parseInt(pageSize);
    let sql="select a.focus_user_id,b.nickname,b.logo,b.introduction," +
        "(SELECT count(f.id) FROM focus_user f where f.focus_user_id=b.user_address) as number_fans,\n" +
        "(SELECT count(s.id) FROM shop s where creator_address=a.focus_user_id and nft_id is not null) as number_nft\n" +
        "from focus_user a left JOIN user b on b.user_address=a.focus_user_id\n" +
        "where a.user_id=? order by b.id desc limit ?,?";
    let pos=(pageIndex-1)*pageSize;
    db.query(sql,[user_address,pos,pageSize],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            //他可以为空
            sql=`select count(a.id) as sum from focus_user a\n` +
            `left JOIN user b on b.user_address=a.focus_user_id\n`
             +`where a.user_id=?`
            db.query(sql,[user_address],function (err,r) {
                if(err){
                    console.log(err);
                    return;
                }else {
                    res.json({data:result,count:r[0].sum})
                }
            })
        }
    })
});

//删除合集
router.post('/delArray', function(req, res, next) {
    let {id}=req.body
    if (id==null){
        res.send({flag:false,msg:'信息不能为空'});
        return;
    }else if (id.trim()==""){
        res.send({flag:false,msg:'信息不能为空'});
        return;
    }
    let sql=`delete from  array where id =?`;
    db.query(sql,[id],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'删除失败'});
            return;
        }else{
            if (result.length<1){
                res.send({flag:false,msg:'删除失败'});
                return;
            }
            sql=`update shop set array_id=null where array_id=?`
            db.query(sql,[id],function (err,result) {
                if(err){
                    console.log(err);
                    res.send({flag:false,msg:'删除失败'});
                    return;
                }else{
                   sql=`delete from collection where shop_id =? and type='array'`
                    db.query(sql,[id],function (err,result) {
                        if(err){
                            console.log(err);
                            res.send({flag:false,msg:'删除失败'});
                            return;
                        }else{
                            res.send({flag:true,msg:'删除成功'});
                        }
                    })
                }
            })
        }
    })
});

//查看自己创建的所有合集
router.post('/getOwnerArray', function(req, res, next) {
    let {pageIndex,pageSize}=req.body
    const token=String(req.headers.authorization).split(' ').pop()
    let user_address=""
    if (token=="undefined"){
        res.send({flag:false,msg:"未登录无权限访问"})
        return;
    }
    let data = Token.decode(token)
    if (data.status=="failed"){
        res.send({flag:false,msg:data.msg})
        return;
    }else {
        user_address=data.data
    }
    if (pageIndex==null||pageSize==null){
        res.send({flag:false,msg:'获取失败'});
        return;
    }else if (pageIndex.trim()==""||pageSize.trim()==""){
        res.send({flag:false,msg:'获取失败'});
        return;
    }
    pageIndex=parseInt(pageIndex);
    pageSize=parseInt(pageSize);
    let pos=(pageIndex-1)*pageSize;
    let sql=`select * from array where owner =? order by id desc limit ?,?`;
    db.query(sql,[user_address,pos,pageSize],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            //他可能没创建合集
            sql=`select count(id) as sum from array where owner =? `
            db.query(sql,[user_address],function (err,r) {
                if(err){
                    console.log(err);
                    return;
                }else {
                    res.json({data:result,count:r[0].sum})
                }
            })
        }
    })
});

// 删除合集的商品
router.post('/delArrayShop', function(req, res, next) {
    let {ids}=req.body
    if (ids==null||ids==""){
        res.send({flag:false,msg:'没有选择删除的商品'});
        return;
    }
    let sql="update shop set array_id=null where id in("+ids+")";
    db.query(sql,[ids],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'删除失败'});
            return;
        }else{
            if (result.length<1){
                res.send({flag:false,msg:'删除失败'});
                return;
            }
            res.send({flag:true,msg:'删除成功'});
        }
    })
});

//添加合集商品
router.post('/AddArrayShop', function(req, res, next) {
    let {shop_ids,array_id}=req.body
    if (shop_ids==null||shop_ids==""){
        res.send({flag:false,msg:'没有选择想要添加的商品'});
        return;
    }
    let sql="update shop set array_id=? where id in ("+shop_ids+")" ;
    db.query(sql,[array_id],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'添加失败'});
            return;
        }else{
            if (result.length<1){
                res.send({flag:false,msg:'添加失败'});
                return;
            }
            res.send({flag:true,msg:'添加合集商品成功'});
        }
    })
});

//获取某个合集的信息和商品
router.post('/getArrayByOne', function (req,r,next) {
    let {id}=req.body
    let p= () => {}
    if (id==null||id.trim()==""){
        r.send({flag:false,msg:'获取失败'});
        return;
    }
    let sql=`select * from array where id=?`;
    db.query(sql,[id],function (err,result) {
        if(err){
            console.log(err);
            r.send({flag:false,msg:'获取失败'});
            return;
        }else{
            if (result.length<1){
                r.send({flag:false,msg:'获取失败'});
                return;
            }
            //商品可能为空 所以不做判断
            sql=`select * from shop where array_id=?`
            var s=result
            p = new Promise((resolve, reject) =>{
                s=addshops(sql,s,id)
                setTimeout(() => {
                    resolve(s)
                }, 100);
            });
        }
        p.then((res) => {
            r.json(res)
        })
    })

});

//查看拒绝原因
router.post('/queryreason', function(req, res, next) {
    let {id}=req.body
    if (id==null||id==""){
        res.send({flag:false,msg:'获取失败'});
        return;
    }
    let sql=`select * from reason where shop_id=?` ;
    db.query(sql,[id],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            if (result.length<1){
                res.send({flag:false,msg:'获取失败'});
                return;
            }
            res.json(result)
        }
    })
});

// 修改密码
router.post('/updatePass', function(req, res, next) {
    let {oldpass,newpass}=req.body
    const token=String(req.headers.authorization).split(' ').pop()
    let user_address=""
    if (token=="undefined"){
        res.send({flag:false,msg:"未登录无权限访问"})
        return;
    }
    let data = Token.decode(token)
    if (data.status=="failed"){
        res.send({flag:false,msg:data.msg})
        return;
    }else {
        user_address=data.data
    }
    if (oldpass==null||newpass==null){
        res.send({flag:false,msg:'密码不能为空或空格'});
        return;
    }else if (oldpass.trim()==""||newpass.trim()==""){
        res.send({flag:false,msg:'密码不能为空或空格'});
        return;
    }
    let sql=`select * from user where password=? and user_address=?` ;
    db.query(sql,[common.md5(oldpass),user_address],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'添加失败'});
            return;
        }else{
            if (result.length>0){
                sql=`update user set password=? where user_address=?`
                db.query(sql,[common.md5(newpass),user_address],function (err,r) {
                    if(err){
                        console.log(err);
                        res.send({flag:false,msg:'添加失败'});
                        return;
                    }else{
                        if (r.length<1){
                            res.send({flag:false,msg:'密码修改失败'});
                            return;
                        }
                        res.send({flag:true,msg:'密码修改成功'});
                    }
                })
            }else {
                res.send({flag:false,msg:'密码错误'});
                return;
            }

        }
    })
});

// 他人的关注列表
router.post('/getOtherFocus', function(req, res, next) {
    let {pageIndex,pageSize,user_address}=req.body
    pageIndex=parseInt(pageIndex);
    pageSize=parseInt(pageSize);
    let sql="select a.focus_user_id,b.nickname,b.logo,b.introduction," +
        "(SELECT count(f.id) FROM focus_user f where f.focus_user_id=b.user_address) as number_fans,\n" +
        "(SELECT count(s.id) FROM shop s where creator_address=a.focus_user_id and nft_id is not null) as number_nft\n" +
        "from focus_user a left JOIN user b on b.user_address=a.focus_user_id\n" +
        "where a.user_id=? order by b.id desc limit ?,?";
    let pos=(pageIndex-1)*pageSize;
    db.query(sql,[user_address,pos,pageSize],function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'获取失败'});
            return;
        }else{
            sql=`select count(a.id) as sum from focus_user a\n` +
                `left JOIN user b on b.user_address=a.focus_user_id\n`
                +`where a.user_id=?`
            db.query(sql,[user_address],function (err,r) {
                if(err){
                    console.log(err);
                    return;
                }else {
                    res.json({data:result,count:r[0].sum})
                }
            })
        }
    })
});

//删除未过审商品
router.post('/delShopByRefushed', function(req, res, next) {
    let {id}=req.body
    if (id==null){
        res.send({flag:false,msg:'选择不能为空'});
        return;
    }
    let sql="delete from shop where id =?"
    db.query(sql,id,function (err,result) {
        if(err){
            console.log(err);
            res.send({flag:false,msg:'没有该商品'});
            return;
        }else{
            if (result.length==0){
                res.send({flag:false,msg:'没有该商品'});
                return;
            }
            sql="delete from reason where shop_id =?"
            db.query(sql,[id],function (err,r) {
                if(err){
                    console.log(err);
                    return;
                }else {
                    if (result.length==1){
                        res.send({flag:true,msg:'删除成功'});
                    }else {
                        res.send({flag:true,msg:'没有该商品'});
                    }
                }
            })
        }
    })
})

module.exports = router;
